package cn.hairui.blog.controller;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.SRescManage;
import cn.hairui.blog.entity.SRescMenu;
import cn.hairui.blog.entity.User;
import cn.hairui.blog.service.SRescMenuService;
import cn.hairui.blog.util.DateUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-08
 */
@Controller

public class SRescMenuController {
    @Autowired
    private SRescMenuService sRescMenuService;

    //// ==========================后台==============================///////
    // 列表页面
    @RequestMapping(value = "/manage/sRescMenu/list", method = RequestMethod.GET)
    public String list() {

        return "page/sRescMenu/list";
    }

    // 新增页面
    @RequestMapping(value = "/manage/sRescMenu/add")
    public String add(Model model) {
        List<SRescManage> sRescManages = sRescMenuService.listManage();
        model.addAttribute("sRescManages", sRescManages);
        return "page/sRescMenu/add";
    }

    // mgrRescmenu修改页面
    @RequestMapping(value = "/manage/sRescMenu/update")
    public String update(HttpServletRequest request, Model model) {
        String rescId = request.getParameter("rescId");
        String href = request.getParameter("href");
        // 加载下拉字典
        List<SRescManage> sRescManages = sRescMenuService.listManage();
        model.addAttribute("sRescManages", sRescManages);

        // 加载数据
        QueryWrapper<SRescMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resc_id", rescId);
        queryWrapper.eq("href", href);
        SRescMenu sRescMenu = (SRescMenu) sRescMenuService.getObj(queryWrapper);

        model.addAttribute("sRescMenu", sRescMenu);
        return "page/sRescMenu/update";
    }

    // 列表数据
    @RequestMapping(value = "/manage/sRescMenu/data")
    @ResponseBody
    public String data(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        List<Map> mapList = sRescMenuService.listMenu();
        map.put("data", mapList);
        map.put("count", mapList.size());
        map.put("code", 0);
        return JSONUtils.toJSONString(map);
    }

    // 新增数据
    @RequestMapping(value = "/manage/sRescMenu/addData")
    @ResponseBody
    public String addData(HttpServletRequest request, SRescMenu sRescMenu) {
        Map map = new HashMap<Object, Object>();
        if (null != sRescMenu.getUseFlag() && PubConstant.ON.equals(sRescMenu.getUseFlag())) {
            sRescMenu.setUseFlag(PubConstant.YES_NO_Y);
        } else {
            sRescMenu.setUseFlag(PubConstant.YES_NO_N);
        }
        sRescMenu.setSpread(false);
        sRescMenuService.saveOrUpdate(sRescMenu);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    // 修改数据
    @RequestMapping(value = "/manage/sRescMenu/updateData")
    @ResponseBody
    public String updateData(HttpServletRequest request, SRescMenu sRescMenu) {
        Map map = new HashMap<Object, Object>();
        if (null != sRescMenu.getUseFlag() && PubConstant.ON.equals(sRescMenu.getUseFlag())) {
            sRescMenu.setUseFlag(PubConstant.YES_NO_Y);
        } else {
            sRescMenu.setUseFlag(PubConstant.YES_NO_N);
        }
        sRescMenuService.saveOrUpdate(sRescMenu);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    // 删除数据
    @RequestMapping(value = "/manage/sRescMenu/delete")
    @ResponseBody
    public String deleteMgrResc(HttpServletRequest request, String rescId, String href) {
        Map map = new HashMap<Object, Object>();
        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (user != null && PubConstant.USER_TYPE_SUPER_ADMIN.equals(user.getType())) {
            Logger.getLogger(String.valueOf(this.getClass()))
                    .info(user.getAccount() + " 于 " + DateUtil.getCurrentTime() + " 删除了资源：" + rescId);
            QueryWrapper<SRescMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("resc_id", rescId);
            queryWrapper.eq("href", href);
            sRescMenuService.remove(queryWrapper);
            map.put(PubConstant.flag, PubConstant.success);
        } else {
            map.put(PubConstant.flag, PubConstant.failed);
        }
        return JSONUtils.toJSONString(map);
    }
}
